<?php
session_start();
include('database.php');


$rating = $_POST['teks0'];
$game_section = $_POST['teks1'];


$xml = simplexml_load_file("feedback.xml");


$query ="select * from rating where section_name='$game_section' and user_id='$_SESSION[user_id]'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows >= 1) { //if already exist or user already rate...need to update the rate point.

$query="update rating set rating='$rating' where user_id='$_SESSION[user_id]' and section_name='$game_section'";
mysqli_query($mysqli,$query);
//change or update the value of rating... update XML rating.xml
foreach ($xml->children() as $child) {
{
if($child->user_id ==$_SESSION[user_id] && $child->section ==$game_section)
{
$child->score = $rating;
}
}
}



}else
{
$rating_id=uniqid("R");
$query="insert into rating (rating_id,user_id,section_name,rating) values ('$rating_id','$_SESSION[user_id]','$game_section','$rating')";
mysqli_query($mysqli,$query);
//add a new line below in rating.xml
$temp = $xml->addChild('rating');
$temp->addChild('section', $game_section);
$temp->addChild('user_id', $_SESSION[user_id]);
$temp->addChild('score', $rating);


}


$xml->asXML("feedback.xml");  

?>